﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	UnioneDialog
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class UnioneDialog : Form
    {
        static public int noFiles;
        static public string[] fileUnione = new string[4];

        public UnioneDialog()
        {
            InitializeComponent();
            string elem = ViewQ.selectedElement;
            string s1 = string.Empty;
            string s2 = string.Empty;
            string s3 = string.Empty;
            string s4 = string.Empty;
            string s5 = string.Empty;
            string s6 = string.Empty;
            string s7 = string.Empty;
            string s8 = string.Empty;
            string s9 = string.Empty;

            int len = elem.Length;

            string numElem = elem.Substring(5, 1);  // di solito len = 6;
            int iret = readFile();
            if (iret != 0)
            {
                MessageBox.Show("impossibile leggere il file " );

            }

            len = elem.Length;

            string codex = elem.Substring(0, 6); ;
            int kk = 0;
            for (kk = 0; kk < nrec; ++kk)
            {
                if (vect[kk].codice.Contains(elem)) break;
            }
            int Xcentro = (vect[kk].P1.X + vect[kk].P3.X )/ 2;
            int Ycentro = (vect[kk].P1.Y + vect[kk].P3.Y) / 2;
            int Xdiff = vect[kk].P3.X - vect[kk].P1.X;
            int Ydiff = vect[kk].P3.Y - vect[kk].P1.Y;

  //          MessageBox.Show(nrec.ToString());

            dataGridView1.ColumnCount = 3;
            this.dataGridView1.Columns[0].HeaderText = "elemento";
            this.dataGridView1.Columns[1].HeaderText = "elemento";
            this.dataGridView1.Columns[2].HeaderText = "elemento";

            this.dataGridView1.Columns[0].Width = 70;
            this.dataGridView1.Columns[1].Width = 70;
            this.dataGridView1.Columns[2].Width = 70;

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;

            Point PP = new Point(0, 0);
                PP.X = Xcentro + Xdiff;
                PP.Y = Ycentro;
                s1 = findElem(PP);
                PP.X = Xcentro + Xdiff;
                PP.Y = Ycentro - Ydiff;
                s2 = findElem(PP);
                PP.X = Xcentro;
                PP.Y = Ycentro - Ydiff;
                s3 = findElem(PP);
                PP.X = Xcentro - Xdiff;
                PP.Y = Ycentro - Ydiff;
                s4 = findElem(PP);
                PP.X = Xcentro - Xdiff;
                PP.Y = Ycentro;
                s5 = findElem(PP);
                PP.X = Xcentro - Xdiff;
                PP.Y = Ycentro + Ydiff;
                s6 = findElem(PP);
                PP.X = Xcentro;
                PP.Y = Ycentro + Ydiff;
                s7 = findElem(PP);
                PP.X = Xcentro + Xdiff;
                PP.Y = Ycentro + Ydiff;
                s8 = findElem(PP);
            string[] row = new string[] { s6, s7, s8 };
            dataGridView1.Rows.Add(row);
            string[] row1 = new string[] { s5, elem, s1 };
            dataGridView1.Rows.Add(row1);
            string[] row2 = new string[] { s4, s3, s2 };
            dataGridView1.Rows.Add(row2);

            dataGridView2.ColumnCount = 2;
            this.dataGridView2.Columns[0].HeaderText = "elemento";
            this.dataGridView2.Columns[1].HeaderText = "nome elemento";

            this.dataGridView2.Columns[0].Width = 60;
            this.dataGridView2.Columns[1].Width = 90;

            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.AllowUserToDeleteRows = false;

            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                column.FlatStyle = FlatStyle.Standard;
                column.ThreeState = false;
                column.CellTemplate = new DataGridViewCheckBoxCell();
                column.CellTemplate.Style.BackColor = Color.White;
                column.HeaderText = "archivio locale";
            }

            dataGridView2.Columns.Insert(2, column);
  
            for (int l = 0;l < 3; ++l)
            {
                for(int k=0;k<3;++k)
                {
                    string ss = dataGridView1[k, l].Value.ToString();
                    ss = ss.Trim();
  //                  MessageBox.Show(ss);
                    string name = findName(ss);
                    dataGridView2.Rows.Add(ss,name,"false");

                }
            }

            string path = FormIniz.cartellaElementi;

            string[] fileEntries = Directory.GetFileSystemEntries(path, "*.zip");
            int count = fileEntries.Count();
            string[] fileEsistenti = new string[count];

            int i = 0;
            foreach (string fileName in fileEntries)
            {
                int ipos = fileName.LastIndexOf('\\');
                ++ipos;
                len = fileName.Length;
                fileEsistenti[i] = fileName.Substring(ipos,len-ipos-4);
                ++i;
            }

            for (i = 0; i < count; ++i)
            {
                if (fileEsistenti[i].Length > 6)
                {
                    string fileN = fileEsistenti[i].Substring(0, 6);
                    int noRows = dataGridView2.RowCount;
              
                    for (int j = 0; j < noRows; ++j)
                    {
                        string ss = dataGridView2[0, j].Value.ToString();
                         if (fileEsistenti[i].Contains(ss) )
                        {
                            string name = dataGridView2[1,j].Value.ToString();
                             dataGridView2.Rows.Insert(j, fileEsistenti[i], name, "true");
                            break;
                        }
                    }
                }          
            }

            for (i = 0; i < count; ++i)
            {
                if (fileEsistenti[i].Length == 6)
                {
                    string fileN = fileEsistenti[i];
                    int noRows = dataGridView2.RowCount;

                    for (int j = 0; j < noRows; ++j)
                    {
                        string ss = dataGridView2[0, j].Value.ToString();
//                        if (fileEsistenti[i].Contains(ss))
                        if (ss.Contains(fileEsistenti[i]))
                        {
                            dataGridView2[2, j].Value = "true";
                        }
                    }
                }
            }

            dataGridView3.ColumnCount = 2;
            this.dataGridView3.Columns[0].HeaderText = "elementi";
            this.dataGridView3.Columns[1].HeaderText = "selezionati";

            this.dataGridView3.Columns[0].Width = 70;
            this.dataGridView3.Columns[1].Width = 70;

            dataGridView3.AllowUserToAddRows = false;
            dataGridView3.AllowUserToDeleteRows = false;

            dataGridView3.Rows.Add("  ", "  ");
            dataGridView3.Rows.Add("  ", "  ");
                    

        }



        struct mDraw
       {
           public string codice;
           public string IDtavol;
           public Point P1;
           public Point P2;
           public Point P3;
           public Point P4;
           public int visib;
       }

       mDraw[] vect = new mDraw[1];
       int Xmin, Xmax, Ymin, Ymax;
       int nrec;


       private int readFile()
       {
           using (SQLiteConnection conn = SetConnection1())
           {
               conn.Open();
               using (SQLiteCommand cmd = new SQLiteCommand(conn))
               {
                   string ss1 = "select count(*) from QU_5000";

                   cmd.CommandText = ss1;
                   // tables:  QU_5000   QU_10000  QU_25000
                   //  campi:  codice nome  X1  Y1  X2  Y2  X3  Y3  X4  Y4

                   nrec = 0;
                   SQLiteDataReader reader1 = cmd.ExecuteReader();
                   if (reader1.HasRows)
                   {
                       while (reader1.Read())
                       {
                           nrec = reader1.GetInt32(0);
                       }
                   }
                   if (nrec == 0)
                   {
                       MessageBox.Show("non trovato alcun elemento");
                       return 1;
                   }
                   reader1.Close();

                   vect = new mDraw[nrec];

                   ss1 = "select * from QU_5000";

                   cmd.CommandText = ss1;
                   SQLiteDataReader reader = cmd.ExecuteReader();
                   if (reader.HasRows)
                   {
                       int i = 0;
                       while (reader.Read())
                       {
                           vect[i].codice = reader.GetString(0).ToString();
                           vect[i].IDtavol = reader.GetString(1).ToString();
                           if (i == 0)
                           {
                               // dim. min-max
                               Xmin = int.Parse(reader.GetString(4).ToString());
                               Ymin = int.Parse(reader.GetString(3).ToString());
                               Xmax = int.Parse(reader.GetString(8).ToString());
                               Ymax = int.Parse(reader.GetString(7).ToString());
                           }
                           //                        else
         //                  {
                               vect[i].P1.X = int.Parse(reader.GetString(2).ToString());
                               vect[i].P1.Y = int.Parse(reader.GetString(3).ToString());
                               vect[i].P2.X = int.Parse(reader.GetString(4).ToString());
                               vect[i].P2.Y = int.Parse(reader.GetString(5).ToString());
                               vect[i].P3.X = int.Parse(reader.GetString(6).ToString());
                               vect[i].P3.Y = int.Parse(reader.GetString(7).ToString());
                               vect[i].P4.X = int.Parse(reader.GetString(8).ToString());
                               vect[i].P4.Y = int.Parse(reader.GetString(9).ToString());
                               vect[i].visib = 1;
         //                  }
                           ++i;
                       }
                   }
               }
               conn.Close();
           }

           return 0;
       }


       private static SQLiteConnection SetConnection1()
       {
           string DataSource = "Data Source=";
           DataSource = string.Concat(DataSource, FormIniz.quadroUnione, ";");
           SQLiteConnection conn = new SQLiteConnection(DataSource);
           return (conn);
       }

        private string findElem(Point PP)
        {   
            int i = 0;
          
            for(i=0;i<nrec;++i)
            {
                if(vect[i].P1.X > PP.X) continue;
                if(vect[i].P3.X < PP.X) continue;
                if(vect[i].P1.Y > PP.Y) continue;
                if(vect[i].P3.Y < PP.Y) continue;
                break;
            }
            if (i == nrec) return ("non trov");
            string ss = vect[i].codice;
    //        MessageBox.Show(i.ToString() + "  "+ss);
            return (ss);

        }

        private string findName(string codex)
        {
            int i = 0;
            for (i = 0; i < nrec; ++i)
            {
                if (vect[i].codice.Contains(codex)) break;
            }
            if(i == nrec) return("non trov");
            return (vect[i].IDtavol);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        static public int callOpenUnione = 0;

        private void button2_Click(object sender, EventArgs e)
        {
            int ipt = 0;
            string s1 = dataGridView3[0, 0].Value.ToString();
            if (s1.Trim().Length > 0) fileUnione[ipt] = s1;
            else fileUnione[ipt] = string.Empty;
            ++ipt;
           
            s1 = dataGridView3[1, 0].Value.ToString();
            if (s1.Trim().Length > 0) fileUnione[ipt] = s1;
            else fileUnione[ipt] = string.Empty;
            ++ipt;

            s1 = dataGridView3[1, 1].Value.ToString();
            if (s1.Trim().Length > 0) fileUnione[ipt] = s1;
            else fileUnione[ipt] = string.Empty;
            ++ipt;

            s1 = dataGridView3[0, 1].Value.ToString();
            if (s1.Trim().Length > 0) fileUnione[ipt] = s1;
            else fileUnione[ipt] = string.Empty;
            ++ipt;

            noFiles = ipt;
            callOpenUnione = 1;
            this.DialogResult = DialogResult.OK;
        }

        int icell = 0;
        int i1, k1;         //   i1  colum
        int i2, k2;         //   k1  row
        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int irow = e.RowIndex;
            string ss = dataGridView2[0, irow].Value.ToString();
             string s2 = dataGridView2[2, irow].Value.ToString();
 //            MessageBox.Show(s2);
             bool bPres = false;
            if(s2.Contains("true")) bPres = true;
            if (!bPres)
            {
                MessageBox.Show("elemento non presente nell'archivio locale");
                return;
            }
            
            int i = 0; int k = 0;
            for (i = 0; i < 3; ++i)
            {
                for (k = 0; k < 3; ++k)
                {
                    string s1 = dataGridView1[i, k].Value.ToString();
                    s1 = s1.Trim();
                    if (ss.Contains(s1)) goto L30;
                }
            }

L30:    //   **********************


            if (icell == 0)
            {
                if (i != 1 || k != 1)
                {
                    MessageBox.Show("iniziare con l'elemento centrale");
                    return;
                }
                dataGridView3[0, 0].Value = ss;
                i1 = i;
                k1 = k;
                ++icell;
                return;
            }
            if (icell == 1)
            {
                if (k1 < k)
                {
                    if (i1 < i)
                    {
                        dataGridView3[1, 1].Value = ss;
                    }

                    if (i1 == i) dataGridView3[0, 1].Value = ss;

                    if (i1 > i)
                    {
                        string s1 = dataGridView3[0, 0].Value.ToString();
                        dataGridView3[1, 0].Value = s1;
                        dataGridView3[0, 1].Value = ss;
                        dataGridView3[0, 0].Value = "  ";
   //                     MessageBox.Show(i1.ToString() + "  " + i.ToString());

                    }
                }

                if (k1 == k)
                {
                    if (i1 < i) dataGridView3[1, 0].Value = ss;
                    else
                    {
                        string s1 = dataGridView3[0, 0].Value.ToString();
                        dataGridView3[0, 0].Value = ss;
                        dataGridView3[1, 0].Value = s1;
                    }
                }

                if (k1 > k)
                {
                    if (i1 < i)
                    {
                        string s1 = dataGridView3[0, 0].Value.ToString();
                        dataGridView3[0, 1].Value = s1;
                        dataGridView3[0, 0].Value = "  ";
                        dataGridView3[1, 0].Value = ss;

                    }
                    if (i1 == i)
                    {
                        string s1 = dataGridView3[0, 0].Value.ToString();
                        dataGridView3[0, 1].Value = s1;
                        dataGridView3[0, 0].Value = ss;
                    }
                    if (i1 > i)
                    {
                        string s1 = dataGridView3[0, 0].Value.ToString();
                        dataGridView3[1, 1].Value = s1;
                        dataGridView3[0, 0].Value = ss;
                    }
                }

                i2 = i;
                k2 = k;
                ++icell;
                return;
            }

            if (icell == 2)
            {
                if (k1 == k2)
                {
                    int  minI, maxI;
                    if (i1 < i2)
                    {
                        minI = i1;
                        maxI = i2;
                    }
                    else 
                    {
                        minI = i2;
                        maxI = i1;
                    }
                    if (i < minI) goto err;
                    if (i > maxI) goto err;

                    if (k1 < k)
                    {
                        if(i == minI)
                        {
                            dataGridView3[0, 1].Value = ss;
                        }
                        else
                        {
                            dataGridView3[1, 1].Value = ss;
                        }
                    }
                    if (k1 > k)
                    {
                        string s1 = dataGridView3[0, 0].Value.ToString();
                        dataGridView3[0, 1].Value = s1;
                        s1 = dataGridView3[1, 0].Value.ToString();
                        dataGridView3[1, 1].Value = s1;
                        if(i == minI)
                        {
                            dataGridView3[0, 0].Value = ss;
                            dataGridView3[1, 0].Value = "  ";

                        }
                        else
                        {
                            dataGridView3[1, 0].Value = ss;
                            dataGridView3[0, 0].Value = "  ";
                        }
                    }
                }

                if (i1 == i2)
                {
                    int minK, maxK;
                    if (k1 < k2)
                    {
                        minK = k1;
                        maxK = k2;
                    }
                    else
                    {
                        minK = k2;
                        maxK = k1;
                    }
                    if (k < minK) goto err;
                    if (k > maxK) goto err;

                    if (i < i1)
                    {
                        string s1 = dataGridView3[0, 0].Value.ToString();
                        dataGridView3[1, 0].Value = s1;
                        s1 = dataGridView3[0, 1].Value.ToString();
                        dataGridView3[1, 1].Value = s1;
                        if (k == k1)
                        {
                            dataGridView3[0, 0].Value = ss;
                            dataGridView3[0, 1].Value = "  ";
                        }
                        if (k == k2)
                        {
                            dataGridView3[0, 1].Value = ss;
                            dataGridView3[0, 0].Value = "  ";
                        }
                    }

                    if (i > i1)
                    {
                        if (k == k1)
                        {
                            dataGridView3[1, 0].Value = ss;
                        }
                        if (k == k2)
                        {
                            dataGridView3[1, 1].Value = ss;
                        }
                    }
                }

                if(i1 != i2 && k1 != k2)
                {
                    if (i2 == 0)
                    {
                        if (i == 2) goto err;
                        if (k2 == 0)
                        {
                            if (i == 0) dataGridView3[0, 1].Value = ss;
                            if (i == 1) dataGridView3[1, 0].Value = ss;
                        }
                        if (k2 == 2)
                        {
                            if (i == 0) dataGridView3[0, 0].Value = ss;
                            if (i == 1) dataGridView3[1, 1].Value = ss;
                        }
                       
                    }
                    if (i2 == 2)
                    {
                        if (i == 0) goto err;
                        if (k2 == 0)
                        {
                            if (i == 1) dataGridView3[0, 0].Value = ss;
                            if (i == 2) dataGridView3[1, 1].Value = ss;
                        }
                        if (k2 == 2)
                        {
                            if (i == 1) dataGridView3[0, 1].Value = ss;
                            if (i == 2) dataGridView3[1, 0].Value = ss;
                        }
                    }
                }
                ++icell;
                return;
            }

            if (icell == 3)
            {
                string s1 = dataGridView3[0, 0].Value.ToString();
                if (s1.Trim().Length == 0)
                {
                    
                    dataGridView3[0, 0].Value = ss;
                    goto ret;
                }
                s1 = dataGridView3[0, 1].Value.ToString();
                if (s1.Trim().Length == 0)
                {

                    dataGridView3[0, 1].Value = ss;
                    goto ret;
                }
                s1 = dataGridView3[1, 0].Value.ToString();
                if (s1.Trim().Length == 0)
                {

                    dataGridView3[1, 0].Value = ss;
                    goto ret;
                }
                s1 = dataGridView3[1, 1].Value.ToString();
                if (s1.Trim().Length == 0)
                {

                    dataGridView3[1, 1].Value = ss;
                    goto ret;
                }

            }
    ret:        return;
            err: MessageBox.Show("elemento non idoneo, ripetere");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView3[0, 0].Value = "  ";
            dataGridView3[1, 0].Value = "  ";
            dataGridView3[0, 1].Value = "  ";
            dataGridView3[1, 1].Value = "  ";
            icell = 0;

        }

        
    }
}
